import axios from "axios";

const isDev = process.env.NODE_ENV === 'development';

const myAxios = axios.create({
    baseURL: isDev ? 'http://localhost:8101/api' : '线上地址',
});

//在请求中都带上cookie
myAxios.defaults.withCredentials = true; // 配置为true

// Add a request interceptor
myAxios.interceptors.request.use(function (config) {
    if(config.timeout === undefined || config.timeout === null){
        config.timeout = 2000;
    }
    const token = sessionStorage.getItem('token')
    if (token) {
        config.headers['Authori-zation'] = token
    }

    config.timeoutErrorMessage = "请求超时，请重试";
    console.log('我要发请求啦', config)
    if(config.url === '/dataManager/gen'){
        console.log('修改请求头')
        /**
         * 这里报传入参数错误，可能设置的请求的头为文件类型，但是我们传入的参数中不只有文件类型的参数，所以这里就会报错。
         * @type {string}
         */
        // config.headers['Content-Type'] = 'multipart/form-data';
    }
    // Do something before request is sent
    return config;
}, function (error) {
    // Do something with request error
    return Promise.reject(error);
});

// Add a response interceptor
myAxios.interceptors.response.use(function (response) {
    console.log('我收到你的响应啦', response)
    // 未登录则跳转到登录页
    // if (response?.data?.code === 40100) {
    //     const redirectUrl = window.location.href;
    //     window.location.href = `/user/login?redirect=${redirectUrl}`;
    // }
    // Do something with response data
    return response.data;
}, function (error) {
    // Do something with response error
    return Promise.reject(error);
});

export default myAxios;
